package com.heu.blood.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

import static com.heu.blood.common.enums.TransfusionApplicationEnum.UN_REVIEWED;

/**
 * 用于自动填充数据库中的gmt_create和gmt_modified字段
 * @Author: lrz
 * @Date: 2023/7/9
 */
@Slf4j        // 用于日志输出
@Component    // 一定不要忘记把处理器添加到 IOC 容器中
public class MyMetaObjectHandler implements MetaObjectHandler {
    // 插入时的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill...");
        // 填充gmt_create和gmt_modified
        this.strictInsertFill(metaObject, "gmtCreate", Date.class, new Date());
        this.strictInsertFill(metaObject, "gmtModified", Date.class, new Date());
        // 填充当前查询时间的字段
        this.strictInsertFill(metaObject, "queryTime", Date.class, new Date());
        // 填充isDeleted字段, 0默认是未删除
        this.strictInsertFill(metaObject, "isDeleted", Integer.class, 0);
        // 填充inspect_time
        this.strictInsertFill(metaObject, "inspectTime", Date.class, new Date());
        // 填充userId(自动生成)
        this.strictInsertFill(metaObject,"userId", String.class, String.valueOf(UUID.randomUUID()));
        // 填充ifShow字段，默认是1，表示可以展示
        this.strictInsertFill(metaObject, "ifShow", Integer.class, 1);
        // 填充统计字段
        this.strictInsertFill(metaObject, "startTransfusionVerification", Integer.class, 0);
        this.strictInsertFill(metaObject, "endTransfusionVerification", Integer.class, 0);
        // 填充checkFlag字段，默认为0，未审核
        this.strictInsertFill(metaObject, "checkFlag", Integer.class, 0);
        this.strictInsertFill(metaObject, "retestFlag", Integer.class, 0);
        this.strictInsertFill(metaObject,"verifyResult",String.class,"Not_Verified");
        //填充transfusionStatus字段，默认为UNREVIEWED，待审核
        this.strictInsertFill(metaObject, "transfusionStatus", String.class, UN_REVIEWED.code());
        //填充是否采血字段，默认为0未采血
        this.strictInsertFill(metaObject, "ifCollected", Integer.class, 0);
//        填充是否打印字段，默认0未打印
        this.strictInsertFill(metaObject, "ifPrint", Integer.class, 0);
    }

    // 更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill...");
        this.strictUpdateFill(metaObject, "gmtModified", Date.class, new Date());
        //更新当前查询时间字段
        this.strictUpdateFill(metaObject, "queryTime", Date.class, new Date());
    }

}

